// pages/tousu/tousu.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      desc:'',
      name:'',
      mobile:'',
      user:'',
      error:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.user && app.globalData.user != null) {
      //获取用户信息
      this.setData({
        user: app.globalData.user,
        mobile: app.globalData.user.mobile
      })
    } else {
      app.emptyUserCallback = user => {
        if (user != null) {
          this.setData({
            user: app.globalData.user,
            mobile: app.globalData.user.mobile
          });

        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  bindName: function (e) {
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  bindDesc: function (e) {
    console.log(e)
    this.setData({
      desc: e.detail.value
    })
  },
  sumitYuyue: function (e) {
    let name = this.data.name
    let desc =this.data.desc
    let mobile = this.data.mobile
    let dateForamt = this.data.dateForamt
    let workshop_id = this.data.workshop_id
    if (name == '') {
      this.setData({
        error: '请输入您的称呼'
      })
      return;
    }
    if (desc == '') {
      this.setData({
        error: '请输入您的投诉的内容'
      })
      return;
    }
    if (this.data.loading == true) {
      return;
    }
    this.setData({
      loading: true
    })
    wx.request({
      url: app.globalData.apiurl + 'tousu',
      method: 'POST',
      data: {
        name,
        mobile,
        desc,
      },
      success: (res) => {
        if (res.data.code == 0) {
          wx.showToast({
            title: res.data.message,
          })
 
          this.setData({
            loading: false,
            name: '',
            mobile: '',
            desc:'',
          })
         wx.redirectTo({
           url: '/pages/index/index',
         })
        } else {
          this.setData({
            error: res.data.message,
            loading: false
          })
        }
      },
      fail: (error) => {
        console.log(error)
        this.setData({
          error: '网络错误',
          loading: false
        })
      }
    })
  },
  //跳转投诉看厂页面
  bindtousu: function (e) {
    let url = '/pages/tousu/tousu'
    if (!this.data.mobile) {
      wx.navigateTo({
        url: '/pages/mobile/mobile?url=' + encodeURIComponent(url),
      })
      return;
    }
    wx.navigateTo({
      url: url,
    })
  },
  //电话
  makeCall: function (e) {

    wx.makePhoneCall({
      phoneNumber: app.globalData.userInfo.mobile
    })
  },
})